package customerEnter.tool;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.collections.map.HashedMap;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import customerEnter.Frame.Utils.CookieUtils;
import customerEnter.Frame.Utils.SystemJSON;
import customerEnter.Frame.Utils.SystemMD5;
import customerEnter.Frame.bean.CustomersInfo;
import customerEnter.Frame.redis.Major.RedisClient;

/**
 * 用戶的信息
 * 
 * 类名称：UserInfoCounts 类描述： 创建人：dy 创建时间：2016-6-15 上午10:37:26
 * 
 */
public class UserInfoCounts {
	/**
	 * spring 獲取request
	 */
	private HttpServletRequest			request;
	private static UserInfoCounts		userInfoCounts	= new UserInfoCounts();
	@SuppressWarnings("unchecked")
	public static Map<String, Integer>	LoveMap			= new HashedMap();

	public static HttpServletRequest getRequest() {

		return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
	}

	public CustomersInfo setCustomers() {
		try {
			request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		} catch (Exception e) {
			System.out.println("request 获取失败");
		}
		if (WhetherToEmpty.ObjectWhetherToEmpty(request)) {
			return new CustomersInfo();
		}

		// CustomersInfo customersInfo = (CustomersInfo)
		// request.getSession().getAttribute("CustomerValue");
		// if (WhetherToEmpty.ObjectWhetherToEmpty(customersInfo)) {
		// return new CustomersInfo();
		// }
		// return customersInfo;
		String token = CookieUtils.getCookieValue(request, "ENYSTRINETI_STRING");
		if (!WhetherToEmpty.StringWhetherToEmpty(token)) {
			token = SystemMD5.EnteMD5(token);
		}
		RedisTemplate<String, String> redisTemplate = springBeans.getBean("redisTemplate");
		String cus = redisTemplate.opsForValue().get("ENYSTRINETI_STRING:" + token);
		redisTemplate.expire("ENYSTRINETI_STRING:" + token, 30, TimeUnit.MINUTES);
		if (WhetherToEmpty.StringWhetherToEmpty(cus)) {
			return null;
		} else {
			JSONObject jsonObject = JSONObject.fromObject(cus);
			CustomersInfo customersInfo = (CustomersInfo) JSONObject.toBean(jsonObject, CustomersInfo.class);
			return customersInfo;
			//
			// JSONArray array = JSONArray.fromObject(cus);
			//
			// List<CustomersInfo> list = SystemJSON.JSONArrayToList(array,
			// CustomersInfo.class);
			// if (WhetherToEmpty.ListWhetherToEmpty(list)) {
			// return new CustomersInfo();
			// } else {
			// return list.get(0);
			// }
		}

	}

	/**
	 * 從socketio 裡面的handshakeData 獲取cookie（請求頭獲取）
	 */
	public static CustomersInfo getCookieCustomersInfo(String socketIOClientHandshakeData) {
		String Cookie = "";
		String[] strings = socketIOClientHandshakeData.split(";");

		for (String string2 : strings) {
			if (string2.contains("ENYSTRINETI_STRING")) {
				String[] strings2 = string2.split("=");
				if (strings2.length > 0) {
					Cookie = strings2[1];
				}
			}
		}
		String token = Cookie;
		if (!WhetherToEmpty.StringWhetherToEmpty(Cookie)) {
			token = SystemMD5.EnteMD5(token);
		}
		String cus = RedisClient.getRedisTemplate().opsForValue().get("ENYSTRINETI_STRING:" + token);
		RedisClient.getRedisTemplate().expire("ENYSTRINETI_STRING:" + token, 30, TimeUnit.MINUTES);

		if (WhetherToEmpty.StringWhetherToEmpty(cus)) {
			return new CustomersInfo();
		} else {
			JSONArray array = JSONArray.fromObject(cus);
			List<CustomersInfo> list = SystemJSON.JSONArrayToList(array, CustomersInfo.class);
			if (WhetherToEmpty.ListWhetherToEmpty(list)) {
				return new CustomersInfo();
			} else {
				return list.get(0);
			}
		}
	}

	/**
	 * 獲取整個用戶類
	 */
	public static CustomersInfo getCustomers() {
		CustomersInfo customers = userInfoCounts.setCustomers();
		if (WhetherToEmpty.ObjectWhetherToEmpty(customers)) {
			customers = new CustomersInfo();
		}
		return customers;
	}

	/**
	 * 獲取id
	 */
	public static int getCustomersId() {
		return getCustomers().getId();
	}

	/**
	 * 活用戶名
	 */
	public static String getCustomersName() {
		return getCustomers().getName();
	}

	/**
	 * 獲取出生日期
	 */
	public static String getBirthday() {
		return getCustomers().getBirthday();
	}

	/**
	 * 獲取性別
	 */
	public static String getSex() {
		return getCustomers().getSex();
	}

	/**
	 * 獲取賬號
	 */
	public static String getAccount() {
		return getCustomers().getAccount();
	}

	/**
	 * 獲取真實名字
	 */
	public static String getRealName() {
		return getCustomers().getRealName();
	}

}
